/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.swing.util;

import javax.swing.Action;

import nemos.swing.IActionProvider;
import nemos.swing.internal.SwingMessage;
import nemos.util.log.NemosLogger;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.util.tracker.ServiceTracker;

/**
 * ActionFinder : The action finder utility class.
 * 
 * <p>
 * This class helps to find actions through the registered
 * {@link IActionProvider} services.
 * </p>
 * 
 * @author Romain DUBOIS
 */
public class ActionFinder {

    /** The service tracker */
    private final ServiceTracker<IActionProvider, IActionProvider> mTracker;

    /** The logger */
    private final NemosLogger mLogger;

    /**
     * Constructor.
     * 
     * @param pContext
     *            The bundle context
     */
    public ActionFinder(final BundleContext pContext) {
        this(pContext, null);
    }

    /**
     * Constructor.
     * 
     * @param pContext
     *            The bundle context
     * @param pReference
     *            The using service reference
     */
    public ActionFinder(final BundleContext pContext, final ServiceReference<?> pReference) {
        super();

        mLogger = new NemosLogger(ActionFinder.class);
        mLogger.setServiceReference(pReference);
        mTracker = new ServiceTracker<IActionProvider, IActionProvider>(pContext,
                IActionProvider.class, null);
        mTracker.open();
    }

    /**
     * Find an action from its ID.
     * 
     * @param pActionId
     *            The action ID
     * @return The identified action, or {@code null} if the action is unknown
     */
    public Action findAction(final String pActionId) {

        // Use the tracked map to ensure the services natural order
        for (final IActionProvider lProvider : mTracker.getTracked().values()) {
            if (lProvider.getAvailableActionIds().contains(pActionId)) {
                mLogger.debug(SwingMessage.ACTION_FOUND, pActionId);
                return lProvider.getAction(pActionId);
            }
        }

        mLogger.warn(SwingMessage.ACTION_MISSING, pActionId);
        return null;
    }

    /**
     * Close the finder.
     */
    public void close() {
        mTracker.close();
    }
}
